package chances.wechat.listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import chances.wechat.model.CollectionDelEvent;
import chances.wechat.model.CollectionEmptyEvent;
import chances.wechat.model.CollectionEvent;
import chances.wechat.model.HistoryDelEvent;
import chances.wechat.model.HistoryEmptyEvent;
import chances.wechat.model.HistoryEvent;
import chances.wechat.service.StbDataService;

@Component
public class DataListener {

    @Autowired
    private StbDataService stbDataService;

    @EventListener
    public void addCollection(CollectionEvent event) {
        if (stbDataService.sendToStb()) {
            stbDataService.addCollection(event.getUserCollection(),
                    event.getStbNo());
        }
    }

    @EventListener
    public void delCollection(CollectionDelEvent event) {
        if (stbDataService.sendToStb()) {
            stbDataService.deleteCollection(event.getStbNo(), event.getCodes());
        }
    }

    @EventListener
    public void emptyCollection(CollectionEmptyEvent event) {
        if (stbDataService.sendToStb()) {
            stbDataService.emptyCollection(event.getStbNo());
        }
    }

    @EventListener
    public void addHistory(HistoryEvent event) {
        if (stbDataService.sendToStb()) {
            stbDataService.addHistory(event.getUserHistory(), event.getStbNo());
        }
    }

    @EventListener
    public void delHistory(HistoryDelEvent event) {
        if (stbDataService.sendToStb()) {
            stbDataService.deleteHistory(event.getStbNo(), event.getCodes());
        }
    }

    @EventListener
    public void emptyHistory(HistoryEmptyEvent event) {
        if (stbDataService.sendToStb()) {
            stbDataService.emptyHistory(event.getStbNo());
        }
    }
}
